package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    //只是负责转发到角色页面
    @RequestMapping("view")
    public String view(){
        return "role";
    }
    /*加载数据的列表页面*/
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo){
        PageResult pageResult = roleService.query(qo);
        return pageResult;
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Role role){
        try {
            if(role.getId() == null) {
                roleService.save(role);
            }else {
                roleService.update(role);
            }
            return JSONResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("保存失败");
        }
    }
    @RequestMapping("listAll")
    @ResponseBody
    public List<Role> listAll(){
        return roleService.listAll();
    }
}
